<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>支付-维多购物平台</title>
    <link rel="shortcut icon" href="./images/favicon.ico" type="image/x-icon" />
    <link href="./bootstrap-3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="./css/style.css">
    <script src="./js/jquery-3.4.1.min.js"></script>
    <script src="./bootstrap-3.4.1/js/bootstrap.min.js"></script>
    <script src="./js/index.js" type="module" defer="defer"></script>
    <script src="./js/cookie.js"></script>
    <style>
        header {
            background-color: rgba(0,0,0,.7);;
            backdrop-filter: blur(20px);
        }

        main {
            padding: 10px;
        }

        .pay-body {
            width: 100%;
            margin: 60px 0 50px 0;
            max-width: 1200px;
        }

        .pay-content {
            width: 100%;
            padding: 20px;
            display: flex;
            flex-direction: column;
            background-color: var(--color-background);
            border-radius: 3px;
            margin-bottom: 40px;
            max-width: 1200px;
            gap: 5px;
        }

        .address-info {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .address-info p {
            width: 20px;
            height: 20px;
            margin: 0;
            margin-left: 10px;
            cursor: pointer;
        }

        .address-info p:hover {
            fill: var(--ui-colors-orange);
        }

        .icon-change {
            width: 100%;
            height: 100%;
        }

        .pay-content table {
            text-align: center;
            width: 100%;
            border-top: 2px solid var(--ui-colors-neutral5);
        }

        .pay-content table th {
            text-align: center;
        }

        .pay-content table td {
            text-align: center;
        }

        .pay-content thead {
            height: 40px;;
        }

        .pay-content tbody {
            width: 100%;
        }

        .pay-content table tr {
            width: 100%;
            border-bottom: 2px solid var(--ui-colors-neutral5);
        }

        table .oid,
        table .price {
            width: 150px;
        }

        table .product {
            display: flex;
            align-items: center;
        }

        table .product img {
            width: 100px;
            height: 100px;
        }

        .pay-all {
            padding: 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            background-color: var(--color-background);
            position: sticky;
            bottom: 0;
            box-shadow: 0 -3px 6px rgba(0,0,0,.1);
        }

        #all-num {
            color: var(--ui-colors-orange);
        }

        .pay-operate {
            color: var(--ui-colors-orange);
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .pay-operate span {
            font-size: 25px;
        }

        .pay-operate p {
            width: 100px;
            height: 30px;
            background-color: var(--ui-colors-orange);
            border-radius: 3px;
            color: var(--color-background);
            margin: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
        }

        h2 {
            font-size: 25px;
            font-weight: 600;
            margin-bottom: 20px;
        }

        h4 {
            font-size: 16px;
            font-weight: 600;
        }

        .price h5 {
            color: var(--ui-colors-orange);
        }

        .modal-body {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }

        .modal-content {
            background-color: var(--color-background-soft);
            padding: 0 10px 10px 10px;
            width: 300px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            gap: 10px;
        }

        .modal-content .title {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 2px solid var(--ui-colors-neutral5);
        }

        .modal-content .content {
            width: 100%;
            display: flex;
            justify-content: center;
            flex-direction: column;
            gap: 10px;
        }

        .modal-content .content span {
            color: var(--ui-colors-orange);
        }

        .modal-content input {
            width: 270px;
            font-size: 14px;
            line-height: 1.5rem;
            min-height: 35px;
            border-radius: 5px;
            padding-inline-start: 10px;
            padding-inline-end: 10px;
            border: 2px solid var(--color-none);
            background-color: var(--ui-colors-neutral3);
            color: var(--color-text);
        }

        .modal-content input:hover {
            background-color: var(--ui-colors-neutral4);
        }

        .modal-content input:focus {
            outline: none;
            border: 2px solid var(--ui-colors-orange);
        }

        .modal-btn-close {
            background-color: var(--color-none);
            border: none;
            font-size: 30px;
        }

        .modal-content .operate {
            display: flex;
            justify-content: flex-end;
            column-gap: 10px;
        }

        .modal-content .operate .btn-primary {
            background-color: var(--ui-colors-orange);
        }

        button:disabled {
            background-color: rgba(255, 92, 0, 0.55);
            cursor: not-allowed;
        }

        .modal-content button:focus {
            display: none;
        }
    </style>
</head>

<body>
    <header id="header" class="has-user">
        <div class="logo">
            <img src="./images/logo.png" alt="logo">
        </div>
        <nav class="main-menu">
            <a href="/">首页</a>
            <a href="/allphone">手机</a>
            <a href="/allcomputer">电脑</a>
        </nav>
        <div class="user-info">
            <div class="user-operate">
                <div class="user-one">
                    <div class="user-name">
                        <span id="username"></span>
                        <svg t="1687403727017" class="icon-user" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="872" width="15" height="15"><path d="M468.7872 738.5088c0-10.4448 4.096-20.8896 12.0832-28.8768L929.792 260.7104c15.9744-15.9744 41.984-15.9744 57.9584 0 7.9872 7.9872 12.0832 18.432 12.0832 28.8768s-4.096 20.8896-12.0832 28.8768L538.624 767.3856c-15.9744 15.9744-41.984 15.9744-57.9584 0-7.9872-7.9872-11.8784-18.432-11.8784-28.8768z" p-id="873"></path><path d="M30.3104 260.9152c7.9872-7.9872 18.432-12.0832 28.8768-12.0832s20.8896 4.096 28.8768 12.0832l448.9216 448.9216c15.9744 15.9744 15.9744 41.984 0 57.9584-7.9872 7.9872-18.432 12.0832-28.8768 12.0832s-20.8896-4.096-29.0816-12.0832L30.3104 318.6688c-15.9744-15.9744-15.9744-41.7792 0-57.7536z" p-id="874"></path></svg>
                    </div>
                    <div class="user-links">
                        <ul>
                            <li><a href="/user"  target="_blank">个人中心</a></li>
                            <li><a href="/wallet"  target="_blank">我的钱包</a></li>
                            <li><a href="/order"  target="_blank">我的订单</a></li>
                            <li><a href="/uevaluate"  target="_blank">评论晒单</a></li>
                            <li><a href="/" onclick="ClearCookie()">退出登录</a></li>
                        </ul>
                    </div>
                </div>
            </div>
            <a href="/trolley" class="mini-shopping-cart">购物车(<span id="count-trolley">0</span>)</a>
        </div>
    </header>
    <main class="pay">
        <div class="pay-body">
            <div class="pay-header">
                <h2>待支付</h2>
            </div>
            <div class="pay-content">
                <h4 class="">收货地址</h4>
                <div class="address-info" id="address-info">
                    <p data-toggle="modal" data-target="#update-address" title="修改地址">
                        <svg t="1687963745165" class="icon-change" viewBox="0 0 1025 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2647" width="25" height="25"><path d="M987.428571 475.428571a36.571429 36.571429 0 0 0-36.571428 36.571429v329.142857a109.714286 109.714286 0 0 1-109.714286 109.714286h-658.285714A109.714286 109.714286 0 0 1 73.142857 841.142857v-658.285714A109.714286 109.714286 0 0 1 182.857143 73.142857H512a36.571429 36.571429 0 0 0 36.571429-36.571428A36.571429 36.571429 0 0 0 512 0H182.857143A182.857143 182.857143 0 0 0 0 182.857143v658.285714A182.857143 182.857143 0 0 0 182.857143 1024h658.285714a182.857143 182.857143 0 0 0 182.857143-182.857143V512a36.571429 36.571429 0 0 0-36.571429-36.571429z" fill="" p-id="2648"></path><path d="M318.171429 707.291429a37.302857 37.302857 0 0 0 51.931428 0L1014.491429 62.171429a35.108571 35.108571 0 0 0 0-51.2 36.571429 36.571429 0 0 0-51.931429 0L318.171429 658.285714a37.302857 37.302857 0 0 0 0 49.005715z" fill="" p-id="2649"></path></svg>
                    </p>
                    <span id="address"></span>
                </div>
                <h4 class="">待支付订单</h4>
                <table>
                    <table role="tree">
                        <thead role="tree">
                            <tr>
                                <th class="oid">
                                    订单号
                                </th>
                                <th class="product-head">商品</th>
                                <th class="price">金额</th>
                            </tr>
                        </thead>
                        <tbody role="tree" id="order-tbody"></tbody>
                    </table>
                </table>
            </div>
            <div class="pay-all">
                <div>共<span id="all-num">0</span>个订单</div>
                <div class="pay-operate">
                    <div>
                        合计：<span id="all-price">0</span>元
                    </div>
                    <p data-toggle="modal" data-target="#go-pay" class="btn-pay">支付</p>
                </div>
            </div>  
        </div>
    </main>
    <div class="modal fade" id="update-address" tabindex="-1" role="dialog"   aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-body">
            <div class="modal-content">
                <div class="title">
                    <h4>修改地址</h4>
                    <button class="modal-btn-close" type="button" data-dismiss="modal"   aria-hidden="true">&times;</button>
                </div>
                <div class="content">
                    <input type="text" placeholder="请输入地址" id="input-address">
                </div>
                <div class="operate">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="btn-pay" onclick="updateAddress()">修改</button>
                </div>
            </div><!--   /.modal-content -->
        </div><!-- /.modal -->
    </div>
    <div class="modal fade" id="go-pay" tabindex="-1" role="dialog"   aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-body">
            <div class="modal-content">
                <div class="title">
                    <h4>支付</h4>
                    <button class="modal-btn-close" type="button" data-dismiss="modal"   aria-hidden="true">&times;</button>
                </div>
                <div class="content">
                    <div>余额：<span id="balance"></span></div>
                    <input type="password" placeholder="请输入密码" id="pwd">
                </div>
                <div class="operate">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="btn-go-pay"  onclick="goPay()">支付</button>
                </div>
            </div><!--   /.modal-content -->
        </div><!-- /.modal -->
    </div>
    <div class="bottom">
        <div class="footer-links clearfix">
            <dl class="col-links col-links-first">
                <dt></dt>
            </dl>
            <dl class="col-links ">
                <dt>选购指南</dt>
                <dd><a rel="nofollow" href="/allphone">手机</a></dd>
                <dd><a rel="nofollow" href="/allcomputer">电脑</a></dd>
            </dl>

            <dl class="col-links ">
                <dt>服务中心</dt>
                <dd><a rel="nofollow" onclick="openChat()" style="cursor: pointer">智能客服</a></dd>
                <dd><a rel="nofollow" href="/order" target="_blank">订单查询</a></dd>
            </dl>

            <dl class="col-links ">
                <dt>关于</dt>
                <dd><a rel="nofollow" href="https://sugarscat.com" target="_blank">了解我们</a></dd>
            </dl>
        </div>
        <div class="site-info">
            <div class="logo ir"></div>
            <div class="info-text">
                <p class="sites">
                    <a rel="nofollow" href="/" target="_blank">维多购物平台</a>
                    <span class="sep">|</span>
                    <a rel="nofollow" href="/privacy" target="_blank"
                    >隐私政策</a
                    >
                </p>
                <p class="sites">
                    <a rel="nofollow" href="http://www.cca.org.cn/" target="_blank"
                    >中国消费者协会</a
                    >
                    <span class="sep">|</span>
                    <a rel="nofollow" href="http://www.bj315.org/" target="_blank"
                    >北京市消费者协会</a
                    >
                </p>
                <p>
                    &copy;2023-至今 版权所有 维多购物平台
                    <br />
                    本网站所列数据、图片，均来自互联网已公开信息，仅供交流学习！
                </p>
            </div>
        </div>
    </div>
</body>
<script src="./js/message.js"></script>
<script src="./js/chat.js"></script>
<script>
    let uid = GetCookie("id")
    let token = GetCookie("token")
    let data = []
    let oids = []
    const address = document.getElementById("address")
    const inputAddress = document.getElementById("input-address")
    let money = 0
    if (token.length>0) {
        $.ajax({
            url:"/api/pay",
            method:"post",
            data:{
                uid: uid,
                token: token
            },
            success(res){
                if(res.code===1001) {
                    data = res.data
                    if (data.length === 0) {
                        location.href = "/"
                        return
                    } 
                    document.getElementById("username").innerText = data[0].username
                    document.getElementById("all-num").innerHTML = data.length.toString()
                    address.innerHTML = data[0].address
                    inputAddress.value = data[0].address
                    let html="";
                    let oidList= []
                    for(let i=0; i < data.length; i++){
                        money += data[i].pprice
                        oidList.push(data[i].oid)
                        html += `<tr>
                            <td class="oid">` + data[i].oid + `</td>
                            <td class="product">
                                <img src="./images/`+ data[i].pclass +`/`+ data[i].pname + `.webp" alt="`+ data[i].pname +`">
                                <h5>`+ data[i].pname + " " + data[i].pversion + " " + data[i].pcolor +`</h5>
                            </td>
                            <td class="price"><h5>`+ data[i].pprice +`元</h5></td></tr>`
                    }
                    $("#order-tbody").append(html)
                    document.getElementById("all-price").innerHTML = money.toString()
                    if (money > parseInt(data[0].wmoney)) {
                        document.getElementById("balance").innerHTML = data[0].wmoney.toString() + " 余额不足！！！"
                        document.getElementById("btn-go-pay").disabled = true

                    } else {
                        document.getElementById("balance").innerHTML = data[0].wmoney.toString()
                        document.getElementById("btn-go-pay").disabled = false
                    }
                    oids = oidList
                } else {
                    AddMessage("warning", "用户令牌过期")
                    setTimeout(()=>{
                        location.href = "/account#login"
                    }, 500)
                }
            }
        })
    } else {
        AddMessage("error", "未登录")
        setTimeout(()=>{
            location.href = "/account#login"
        }, 500)
    }

    function updateAddress() {
        const Iaddress = inputAddress.value
        if (Iaddress.length === 0) {
            AddMessage("warning", "地址不可为空")
            return
        }
        $.ajax({
            url:"/api/payupdate",
            method:"post",
            data:{
                uid: uid,
                token: token,
                address: Iaddress
            },
            success(res){
                if(res.code===1001) {
                    AddMessage("success", "修改成功")
                    address.innerHTML = Iaddress
                    $("#update-address").modal('hide') //关闭模态框
                    if (Iaddress.length > 0) {
                        document.getElementById("address-info").style.backgroundColor = "var(--color-none)"
                    }
                } else {
                    AddMessage("warning", "用户令牌过期")
                    setTimeout(()=>{
                        location.href = "/account#login"
                    }, 500)
                }
            }
        })
    }

    function goPay() {
        let oidString = ""
        for (let i = 0; i < oids.length; i++) {
            oidString += oids[i]
            if (i < oids.length - 1)
                oidString += "$"
        }
        if (address.innerHTML.length > 0) {
            $.ajax({
                url:"/api/paygo",
                method:"post",
                data:{
                    uid: uid,
                    token: token,
                    oid: oidString,
                    money: money,
                    pwd: document.getElementById("pwd").value
                },
                success(res){
                    if(res.code===1001) {
                        AddMessage("success", "支付成功")
                        location.href = "/order"
                    } else if (res.code===1002) {
                        AddMessage("warning", "余额不足")
                        setTimeout(()=>{
                            location.href = "/wallet"
                        }, 500)
                    } else if (res.code===1003) {
                        AddMessage("warning", "密码错误")
                    } else if (res.code===1004) {
                        AddMessage("warning", "地址不可为空")
                        $("#go-pay").modal('hide') //关闭模态框
                        document.getElementById("address-info").style.backgroundColor = "var(--ui-colors-danger9)"
                    } else if (res.code===1004) {
                        AddMessage("warning", "用户令牌过期")
                        setTimeout(()=>{
                            location.href = "/account#login"
                        }, 500)
                    }
                }
            })
        } else {
            AddMessage("warning", "地址不可为空")
            $("#go-pay").modal('hide') //关闭模态框
            document.getElementById("address-info").style.backgroundColor = "var(--ui-colors-danger9)"
        }
    }
</script>
</html>